<?php
header('Content-type:text/html;charset=utf-8');
session_start();
include_once("../class/mysqlclass.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$tle = trim($_POST['title']);
	$_SESSION['tle']=$tle;
	$ath = $_POST['author'];
	$_SESSION['ath']=$ath;
	$dsc = trim($_POST['description']);
	$_SESSION['dsc']=$dsc;
	$con = trim($_POST['content']);
	$_SESSION['con']=$con;
	//时间函数
    $pdo=new my();
    $time = $pdo->GetChinaTime();
    if (empty(Addslashes($tle))){
        echo "<script> alert('文章标题不能为空！');location.href='write.php';</script>";
		exit();
    }else {
        if (empty(Addslashes($ath))){
            echo "<script> alert('文章作者不能为空！');location.href='write.php';</script>";
            exit();
        }else{
            if (empty(Addslashes($dsc))){
                echo "<script> alert('文章简介不能为空！');location.href='write.php';</script>";
		        exit();
            }else{
                if (empty(Addslashes($con))){
                    echo "<script> alert('文章内容不能为空！');location.href='write.php';</script>";
		            exit();
                }
            }
        }
    }   
}
$mysqli = new writing;
$mysqli = $mysqli -> Con();
$pdo=new my();
$num=$pdo->NumAdd();
$sql = "INSERT INTO `write` ( `title`, `author`, `description`, `content`, `num`,`time`) VALUES ('{$tle}', '{$ath}', '{$dsc}', '{$con}', '{$num}','{$time}');";
if($mysqli->query($sql)){
    echo "<script>alert('已经编写成功！');location.href='wenz.php';</script>";
    $_SESSION['ath']=$ath;
    $_SESSION['con']=NULL;
    $_SESSION['tle']=NULL;
    $_SESSION['dsc']=NULL;
}else{
	//报错，保存草稿
    echo "<script>alert('错误！');location.href='write.php';</script>";
    $_SESSION['ath']=$ath;
    $_SESSION['con']=$con;
    $_SESSION['tle']=$tle;
    $_SESSION['dsc']=$dsc;
}
?>